package fpt.edu.vn.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.Districts;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.model.Topicimages;
import fpt.edu.vn.model.Topics;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class CheckEditShareTopicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;
	private String Username;
	private List<Provinces> lstProvince;
	private List<Districts> lstDistrict;
	private int provinceTopic;
	private int districtTopic;
	private int isOwner;
	private int isAdmin;
	private Topics topic = new Topics();
	private int topicId;
	private AddnewDao addnewdao = new AddnewDao();
	private TopicsDao topicdao = new TopicsDao();
	private List<Topicimages> lstimages;
	private int temptopicId;

	public int getTemptopicId() {
		return temptopicId;
	}

	public void setTemptopicId(int temptopicId) {
		this.temptopicId = temptopicId;
	}

	// For SessionAware
	Map<String, Object> session;

	public String execute() {
		String email = "";
		session = ActionContext.getContext().getSession();
		String displayNamess = "";
		if (session.containsKey("emailss")) {
			displayNamess = (String) session.get("displayNamess");
			if (topicId != 0) {
				session.put("topicIdEditss", topicId);
			}
			if (session.containsKey("emailss")) {
				topicId = (Integer) session.get("topicIdEditss");
			}
			if ("".equals(displayNamess) || displayNamess == null) {
				setUsername(null);
			} else {
				setUsername(displayNamess);
			}
			email = (String) session.get("emailss");
			// get email, userid to compare with user login
			List<Object> checkOwnerTopic = topicdao.checkOwnerTopic(email,
					topicId);
			List<Object> checkAdmin = topicdao.checkAdmin(email);
			if (checkAdmin.size() < 1) {
			} else {
				setIsAdmin(1);
			}
			if (checkOwnerTopic.size() < 1) {
			} else {
				setIsOwner(1);
			}

		}
		if (isAdmin == 1 || isOwner == 1) {
			if (topicId == 0 || topicdao.detail(topicId).size() == 0) {
				return NONE;
			} else {
				List<Topics> lsttopic = topicdao.topicinfo(topicId);
				topic = lsttopic.get(0);
				lstimages = topicdao.image(topicId);
				// get province list
				this.lstProvince = addnewdao.getProvince();
				// get province ID of topic
				districtTopic = topic.getDistrictId();
				if (districtTopic != 0) {
					List<Districts> lstdistemp = addnewdao
							.getProvinceInfo(districtTopic);
					provinceTopic = lstdistemp.get(0).getProvinceId();
				}
				// get district list
				this.lstDistrict = addnewdao.getDistricts(provinceTopic);
				return SUCCESS;
			}
		} else {
			return ERROR;
		}

	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}

	public List<Topicimages> getLstimages() {
		return lstimages;
	}

	public void setLstimages(List<Topicimages> lstimages) {
		this.lstimages = lstimages;
	}

	public Topics getTopic() {
		return topic;
	}

	public void setTopic(Topics topic) {
		this.topic = topic;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getIsOwner() {
		return isOwner;
	}

	public void setIsOwner(int isOwner) {
		this.isOwner = isOwner;
	}

	public int getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(int isAdmin) {
		this.isAdmin = isAdmin;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	public List<Provinces> getLstProvince() {
		return lstProvince;
	}

	public void setLstProvince(List<Provinces> lstProvince) {
		this.lstProvince = lstProvince;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public TopicsDao getTopicdao() {
		return topicdao;
	}

	public void setTopicdao(TopicsDao topicdao) {
		this.topicdao = topicdao;
	}

	public List<Districts> getLstDistrict() {
		return lstDistrict;
	}

	public void setLstDistrict(List<Districts> lstDistrict) {
		this.lstDistrict = lstDistrict;
	}

	public int getProvinceTopic() {
		return provinceTopic;
	}

	public void setProvinceTopic(int provinceTopic) {
		this.provinceTopic = provinceTopic;
	}

	public int getDistrictTopic() {
		return districtTopic;
	}

	public void setDistrictTopic(int districtTopic) {
		this.districtTopic = districtTopic;
	}

}
